import 'package:flutter/material.dart';
import '../../service/api_method.dart';
import './widgets/home_swiper.dart';
import './widgets/top_navigator.dart';
import './widgets/ad_banner.dart';
import './widgets/leader_phone.dart';
import './widgets/recommend.dart';
import './widgets/floor.dart';
import './widgets/hot_zone.dart';
import 'dart:convert';
import 'package:flutter_easyrefresh/easy_refresh.dart';

class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with AutomaticKeepAliveClientMixin {
  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  List hotGoodsList = [];
  var page = 1;

  @override
  Widget build(BuildContext context) {
    const params = {'lon': '115.02932', 'lat': '35.76189'};
    return Scaffold(
      appBar: AppBar(title: Text('百姓生活+')),
      backgroundColor: Color.fromRGBO(244, 245, 245, 1.0),
      body: FutureBuilder(
          future: request('homePageContent', params: params),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            if (snapshot.hasData) {
              var data = json.decode(snapshot.data);
              // 轮播图数据
              var slides = data['data']['slides'];
              // 导航数据
              var categories = data['data']['category'];
              // 广告图片数据
              var advertesPicture =
                  data['data']['advertesPicture']['PICTURE_ADDRESS'];
              // 联系店长
              var leaderImage = data['data']['shopInfo']['leaderImage'];
              var leaderPhone = data['data']['shopInfo']['leaderPhone'];
              // 推荐数据
              var recommendList = data['data']['recommend'];
              // 获取三个楼层数据
              var floor1Title = data['data']['floor1Pic']['PICTURE_ADDRESS'];
              var floor2Title = data['data']['floor2Pic']['PICTURE_ADDRESS'];
              var floor3Title = data['data']['floor3Pic']['PICTURE_ADDRESS'];
              var floor1List = data['data']['floor1'];
              var floor2List = data['data']['floor2'];
              var floor3List = data['data']['floor3'];
              return EasyRefresh(
                footer: ClassicalFooter(
                    bgColor: Colors.white,
                    textColor: Colors.pinkAccent,
                    infoColor: Colors.pinkAccent,
                    infoText: '加载中...',
                    loadingText: '上拉加载',
                    noMoreText: ''),
                child: ListView(
                  children: <Widget>[
                    HomeSwiper(slides: slides),
                    TopNavigator(categories: categories),
                    AdBanner(advertesPicture: advertesPicture),
                    LeaderPhone(
                        leaderImage: leaderImage, leaderPhone: leaderPhone),
                    Recommend(recommendList: recommendList),
                    Floor(floorTitlePic: floor1Title, floorList: floor1List),
                    Floor(floorTitlePic: floor2Title, floorList: floor2List),
                    Floor(floorTitlePic: floor3Title, floorList: floor3List),
                    HotZone(hotGoodsList: hotGoodsList)
                  ],
                ),
                onLoad: () async {
                  // print('开始加载更多了');
                  var formPage = {'page': page};
                  await request('homePageBelowConten', params: formPage)
                      .then((val) {
                    var data = json.decode(val.toString());
                    if (data == null) return;
                    List<Map> newGoodsList = (data['data'] as List).cast();
                    setState(() {
                      hotGoodsList.addAll(newGoodsList);
                      page = page + 1;
                    });
                  });
                },
              );
            } else {
              return Container(
                  alignment: Alignment.center, child: Text('数据加载中...'));
            }
          }),
    );
  }
}
